package com.cgx.tablet.beeper.fragment;


import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.cgx.tablet.beeper.MyApplication;
import com.cgx.tablet.beeper.R;
import com.cgx.tablet.beeper.adapter.WinTransAdapter;
import com.cgx.tablet.beeper.base.BaseFragment;
import com.cgx.tablet.beeper.bean.CallBean;
import com.cgx.tablet.beeper.bean.WinTransBean;
import com.cgx.tablet.beeper.databinding.FragmentWinTransBinding;
import com.cgx.tablet.beeper.event.TicketTransEvent;
import com.cgx.tablet.beeper.presenter.WinTransPresenter;
import com.cgx.tablet.beeper.utils.ToastUtil;
import com.cgx.tablet.beeper.view.IWinTransView;
import com.cgx.tablet.beeper.widget.decoration.LastItemDecoration;
import com.yatoooon.screenadaptation.ScreenAdapterTools;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class WinTransFragment extends BaseFragment implements View.OnClickListener, WinTransAdapter.OnItemClickListener, IWinTransView {


    private FragmentWinTransBinding mBinding;
    private WinTransAdapter adapter;
    private List<WinTransBean> winTransBeans = new ArrayList<>();
    private int pos = -1;
    private WinTransPresenter winTransPresenter;
    private CallBean callBean;

    public static WinTransFragment newInstance(CallBean callBean) {
        WinTransFragment winTransFragment = new WinTransFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("callBean", callBean);
        winTransFragment.setArguments(bundle);
        return winTransFragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_win_trans;
    }

    @Override
    protected void bind(View view) {
        mBinding = DataBindingUtil.bind(view);
        winTransPresenter = new WinTransPresenter(this);
        Bundle bundle = getArguments();
        if (bundle != null)
            callBean = bundle.getParcelable("callBean");
        else
            callBean = new CallBean();
        ScreenAdapterTools.getInstance().loadView(view);
    }

    @Override
    protected void onLazyLoad() {
        initAdapter();
        initData();
    }

    private void initAdapter() {
        adapter = new WinTransAdapter(getContext(), winTransBeans);
        mBinding.windowTransferRv.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter.setOnItemClickListener(this);
        mBinding.transfer.setOnClickListener(this);
        LastItemDecoration itemDecoration = new LastItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.diver_1px));
        mBinding.windowTransferRv.addItemDecoration(itemDecoration);
        mBinding.windowTransferRv.setAdapterWithProgress(adapter);
    }

    private void initData() {
        winTransPresenter.getWins(callBean.getBizId());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.transfer:
                if (pos != -1) {
                    winTransPresenter.winTrans(callBean.getTid(), winTransBeans.get(pos).getWinId(), mBinding.transPrior.isChecked());
                } else
                    ToastUtil.showShort(getContext(), R.string.select_win_trans);
                break;
        }
    }

    @Override
    public void onItemClick(View view, int pos, List<WinTransBean> mData) {
        for (int i = 0; i < mData.size(); i++) {
            mData.get(i).setChecked(false);
        }
        WinTransBean winTransBean = mData.get(pos);
        winTransBean.setChecked(true);
        this.pos = pos;
        adapter.notifyDataSetChanged();
    }

    @Override
    public void getWinsSuccess(List<WinTransBean> winTransBeans) {
        this.winTransBeans.clear();
        this.winTransBeans.addAll(winTransBeans);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void getWinsFail(String code, String msg) {
        ToastUtil.showShort(getContext(), msg);
    }

    @Override
    public void winTransSuccess() {
        EventBus.getDefault().post(new TicketTransEvent());
    }

    @Override
    public void winTransFail(String code, String msg) {
        ToastUtil.showShort(MyApplication.getGlobalContext(), msg);
    }
}
